import RPi.GPIO as GPIO
import time

# use physical pin numbers
GPIO.setmode(GPIO.BOARD)

# switch off GPIO warnings
GPIO.setwarnings(False)

output_pin = [3,5,7,8,10,11,12,13]

def init():
  # setup output pins, switch pins off
  for i in output_pin:
    GPIO.setup(i,GPIO.OUT,initial=0)  

def description():
  print('LED strip pidget methods:')
  print('  init():               initializes output pins')
  print('  set_led([0,7],[0,1]): sets LED #0-7 to off or on')
  print('  set_binary([0,255]):  LEDs show binary representation of given number')
  print('  get_led_states():     returns state of LEDs as integer')
  print('  test():               flashes all 8 LEDs for 1 second')

def set_led(number,status):
  try:
    GPIO.output(output_pin[int(number)],int(status))
  except ValueError:
    print ('Error: number/status is not an integer')
  except IndexError:
    print ('Error: number not in range [0,7]')
    
def set_binary(number):
  try:
    number=int(number)
    if (number>=0 and number<256):
      for i in output_pin:
        GPIO.output(i,0)
      i=0
      while number!=0:
        set_led(i,number%2)
        number = int(number/2)
        i = i+1        
    else:
      print('Error: number not in range [0,255]')
      # switch off all LEDs
      set_binary(0)
  except ValueError:
    print ('Error: number not an integer')

def get_led_states():
  state = 0
  for i in range (0,8):
    state = state + GPIO.input(output_pin[i]) * 2**i
  return state

def test():
  for i in output_pin:
    GPIO.output(i,1)
  time.sleep(1)
  for i in output_pin:
    GPIO.output(i,0)
